home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Image / GIS / Renderer.php < prev   
PHP Script  |  2004-03-24  |  7KB  |  268 lines

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: Image :: GIS :: Renderer Base Class                            |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2004 Jan Kneschke <jan@kneschke.de> and             |
  7. // |                         Sebastian Bergmann <sb@sebastian-bergmann.de>. |
  8. // +------------------------------------------------------------------------+
  9. // | This source file is subject to version 3.00 of the PHP License,        |
  10. // | that is available at http://www.php.net/license/3_0.txt.               |
  11. // | If you did not receive a copy of the PHP license and are unable to     |
  12. // | obtain it through the world-wide-web, please send a note to            |
  13. // | license@php.net so we can mail you a copy immediately.                 |
  14. // +------------------------------------------------------------------------+
  15. //
  16. // $Id: Renderer.php,v 1.9 2004/01/01 10:31:37 sebastian Exp $
  17. //
  18.  
  19. require_once 'Image/Color.php';
  20.  
  21. /**
  22. * Renderer Base Class.
  23. *
  24. * @version  $Revision: 1.9 $
  25. * @since    Image_GIS 1.0.0
  26. */
  27. class Image_GIS_Renderer {
  28.     /**
  29.     * Set to TRUE to enable debugging.
  30.     *
  31.     * @var boolean $debug
  32.     */
  33.     var $debug;
  34.  
  35.     /**
  36.     * @var array $min
  37.     */
  38.     var $min = false;
  39.  
  40.     /**
  41.     * @var array $max
  42.     */
  43.     var $max = false;
  44.  
  45.     /**
  46.     * Width of the image.
  47.     *
  48.     * @var integer $width
  49.     */
  50.     var $width;
  51.  
  52.     /**
  53.     * Height of the image.
  54.     *
  55.     * @var integer $height
  56.     */
  57.     var $height;
  58.  
  59.     /**
  60.     * Constructor.
  61.     *
  62.     * @param  mixed   $width
  63.     * @param  integer $height
  64.     * @param  boolean $debug
  65.     * @access public
  66.     */
  67.     function Image_GIS_Renderer($width, $height, $debug) {
  68.         $this->debug  = $debug;
  69.  
  70.         if ($width < 0 ||
  71.             $width > 2048) {
  72.             $width = 640;
  73.         }
  74.  
  75.         if ($height < 0 ||
  76.             $height > 2048) {
  77.             $height = 480;
  78.         }
  79.  
  80.         $this->width  = $width;
  81.         $this->height = $height;
  82.    }
  83.  
  84.     /**
  85.     * Factory.
  86.     *
  87.     * @param  string  $renderer
  88.     * @param  mixed   $width
  89.     * @param  integer $height
  90.     * @param  boolean $debug
  91.     * @return object
  92.     * @access public
  93.     */
  94.     function &factory($renderer, $width, $height, $debug) {
  95.         if (@include_once('Image/GIS/Renderer/' . $renderer . '.php')) {
  96.             $class  = 'Image_GIS_Renderer_' . $renderer;
  97.             $object = new $class($width, $height, $debug);
  98.  
  99.             return $object;
  100.         }
  101.     }
  102.  
  103.     /**
  104.     * Draws a clipped line from ($x1, $y1) to ($x2, $y2)
  105.     * using $color.
  106.     *
  107.     * @param  float $x1
  108.     * @param  float $y1
  109.     * @param  float $x2
  110.     * @param  float $y2
  111.     * @param  mixed $color
  112.     * @access public
  113.     */
  114.     function drawClippedLine($x1, $y1, $x2, $y2, $color) {
  115.         if (($x1 > $this->max['x']  ||
  116.              $x1 < $this->min['x']  ||
  117.              $y1 > $this->max['y']  ||
  118.              $y1 < $this->min['y']) &&
  119.             ($x2 > $this->max['x']  ||
  120.              $x2 < $this->min['x']  ||
  121.              $y2 > $this->max['y']  ||
  122.              $y2 < $this->min['y'])) {
  123.             if ($this->debug) {
  124.                 printf('clipped x1: %d %d %d<br />', $x1, $this->min['x'], $this->max['x']);
  125.                 printf('clipped y1: %d %d %d<br />', $y1, $this->min['y'], $this->max['y']);
  126.                 printf('clipped x2: %d %d %d<br />', $x2, $this->min['x'], $this->max['x']);
  127.                 printf('clipped y2: %d %d %d<br />', $y2, $this->min['y'], $this->max['y']);
  128.             }
  129.         } else {
  130.             if (!is_array($color)) {
  131.                 $color = Image_Color::namedColor2RGB($color);
  132.             }
  133.  
  134.             $x1 = $this->polar2image($x1, 'x');
  135.             $y1 = $this->polar2image($y1, 'y');
  136.             $x2 = $this->polar2image($x2, 'x');
  137.             $y2 = $this->polar2image($y2, 'y');
  138.  
  139.             if ($this->debug) {
  140.                 printf('Drawing line (%s, %s) -> (%s, %s)<br />', $x1, $y1, $x2, $y2);
  141.             }
  142.  
  143.             $this->drawLine($x1, $y1, $x2, $y2, $color[0], $color[1], $color[2]);
  144.         }
  145.     }
  146.  
  147.     /**
  148.     * Returns the range of the data to be rendered.
  149.     *
  150.     * @return array
  151.     * @access public
  152.     * @since  Image_GIS 1.0.1
  153.     */
  154.     function getRange() {
  155.         return array(
  156.           $this->min['x'],
  157.           $this->max['x'],
  158.           $this->min['y'],
  159.           $this->max['y']
  160.         );
  161.     }
  162.  
  163.     /**
  164.     * Converts a polar coordinate to an image coordinate.
  165.     *
  166.     * @param  float  $polarCoordinate
  167.     * @param  string $direction
  168.     * @access public
  169.     */
  170.     function polar2image($polarCoordinate, $direction) {
  171.         switch ($direction) {
  172.             case 'x': {
  173.                 return ($polarCoordinate - $this->min[$direction]) *
  174.                        ($this->width / ($this->max[$direction] - $this->min[$direction]));
  175.             }
  176.             break;
  177.  
  178.             case 'y': {
  179.                 return ($polarCoordinate - $this->max[$direction]) *
  180.                        ($this->height / ($this->min[$direction] - $this->max[$direction]));
  181.             }
  182.             break;
  183.         }
  184.     }
  185.  
  186.     /**
  187.     * Renders the image.
  188.     *
  189.     * @param  array $lineSets
  190.     * @access public
  191.     */
  192.     function render($lineSets) {
  193.         if ($this->min == false || $this->max == false) {
  194.             $this->min = array(
  195.               'x' => 0,
  196.               'y' => 0
  197.             );
  198.  
  199.             $this->max = array(
  200.               'x' => 0,
  201.               'y' => 0
  202.             );
  203.  
  204.             foreach ($lineSets as $lineSet) {
  205.                 $this->min['x'] = min($this->min['x'], $lineSet->min['x']);
  206.                 $this->min['y'] = min($this->min['y'], $lineSet->min['y']);
  207.                 $this->max['x'] = max($this->max['x'], $lineSet->max['x']);
  208.                 $this->max['y'] = max($this->max['y'], $lineSet->max['y']);
  209.             }
  210.         }
  211.  
  212.         foreach ($lineSets as $lineSet) {
  213.             foreach ($lineSet->lines as $line) {
  214.                 $this->drawClippedLine($line[0], $line[1], $line[2], $line[3], $lineSet->color);
  215.             }
  216.         }
  217.     }
  218.  
  219.     /**
  220.     * Sets the range of the data to be rendered.
  221.     *
  222.     * @param  float $x1
  223.     * @param  float $x2
  224.     * @param  float $y1
  225.     * @param  float $y2
  226.     * @access public
  227.     */
  228.     function setRange($x1, $x2, $y1, $y2) {
  229.         $this->min = array('x' => $x1, 'y' => $y1);
  230.         $this->max = array('x' => $x2, 'y' => $y2);
  231.     }
  232.  
  233.     /**
  234.     * Draws a line from ($x1, $y1) to ($x2, $y2)
  235.     * using the color rgb($r, $g, $b).
  236.     *
  237.     * @param  float   $x1
  238.     * @param  float   $y1
  239.     * @param  float   $x2
  240.     * @param  float   $y2
  241.     * @param  float   $r
  242.     * @param  float   $g
  243.     * @param  float   $b
  244.     * @access public
  245.     * @abstract
  246.     */
  247.     function drawLine($x1, $y1, $x2, $y2, $r, $g, $b) { /* abstract */ }
  248.  
  249.     /**
  250.     * Saves the rendered image to a given file.
  251.     *
  252.     * @param  string  $filename
  253.     * @return boolean
  254.     * @access public
  255.     * @abstract
  256.     */
  257.     function saveImage($filename) { /* abstract */ }
  258.  
  259.     /**
  260.     * Shows the rendered image.
  261.     *
  262.     * @access public
  263.     * @abstract
  264.     */
  265.     function showImage() { /* abstract */ }
  266. }
  267. ?>
  268.